﻿class ColorTransformLite extends MovieClip {
	static var DEPTH_LOADER:Number = 2;
	
	static var DEPTH_CCSC_R:Number = 3;
	static var DEPTH_CCSC_G:Number = 4;
	static var DEPTH_CCSC_B:Number = 5;
	static var DEPTH_CCSC_A:Number = 6;
	
	var __loader:MovieClip;
	var __mcl:MovieClipLoader;
	
	var __controls:Array;
	var __idxFocusedControl:Number;
	
	var __keyListener:Object;
	
	function ColorTransformLite() {
		fscommand2("FullScreen", true);
		
		init();
		initColorChannelSliderControl();
	}
	
	function init():Void {
		__loader = this.createEmptyMovieClip("loader_mc", DEPTH_LOADER);

		__mcl = new MovieClipLoader();
		__mcl.addListener(this);
		
		__mcl.loadClip("ColorTransformLite_photo.jpg", __loader);

		__keyListener = {onKeyDown: mx.utils.Delegate.create(this, onKeyDown)};
		Key.addListener(__keyListener);
	}
	
	function onKeyDown():Void{
		switch(Key.getCode()){
			case Key.UP:
				setFocusedControl(__idxFocusedControl-1);
				break;
			case Key.DOWN:
				setFocusedControl(__idxFocusedControl+1);
				break;
			case Key.LEFT:
				addControlValue(-5);
				break;
			case Key.RIGHT:
				addControlValue(5);
				break;
			case "1".charCodeAt(0):
				startAutoTransform();
				break;
			case "2".charCodeAt(0):
				stopAutoTransform();
				break;
		}
	}
	
	function initColorChannelSliderControl():Void{
		var __ccsc_r:ColorChannelSliderControl = new ColorChannelSliderControl(this.createEmptyMovieClip("ccsc_r_mc", DEPTH_CCSC_R), 150, 10, 0xff0000);
		var __ccsc_g:ColorChannelSliderControl = new ColorChannelSliderControl(this.createEmptyMovieClip("ccsc_g_mc", DEPTH_CCSC_G), 150, 10, 0x00ff00);
		var __ccsc_b:ColorChannelSliderControl = new ColorChannelSliderControl(this.createEmptyMovieClip("ccsc_b_mc", DEPTH_CCSC_B), 150, 10, 0x0000ff);
		var __ccsc_a:ColorChannelSliderControl = new ColorChannelSliderControl(this.createEmptyMovieClip("ccsc_a_mc", DEPTH_CCSC_A), 150, 10, 0xffffff);
		
		__controls = [__ccsc_r, __ccsc_g, __ccsc_b, __ccsc_a];
		setFocusedControl(0);
		
		this["ccsc_r_mc"]._x = 120
		this["ccsc_r_mc"]._y = 250;
		
		this["ccsc_g_mc"]._x = this["ccsc_r_mc"]._x;
		this["ccsc_g_mc"]._y = this["ccsc_r_mc"]._y + this["ccsc_r_mc"]._height + 5;
		
		this["ccsc_b_mc"]._x = this["ccsc_g_mc"]._x;
		this["ccsc_b_mc"]._y = this["ccsc_g_mc"]._y + this["ccsc_g_mc"]._height + 5;
		
		this["ccsc_a_mc"]._x = this["ccsc_b_mc"]._x;
		this["ccsc_a_mc"]._y = this["ccsc_b_mc"]._y + this["ccsc_b_mc"]._height + 5;
	}
	
	function onLoadInit(target_mc:MovieClip):Void{

	}
	
	function setFocusedControl(idx:Number):Void{
		while(idx<0){
			idx+=__controls.length;
		}
		while(idx>=__controls.length){
			idx-=__controls.length;
		}
		__idxFocusedControl = idx;
		for(var i:Number=0; i<__controls.length; i++){
			ColorChannelSliderControl(__controls[i]).setFocus(i==idx);
		}
	}
	
	function addControlValue(dv:Number):Void{
		if(onEnterFrame == onEnterFrame_autoTransform){
			stopAutoTransform();
		}
		
		__controls[__idxFocusedControl].value += dv;
		
		updatePhotoColorTransform();
	}
	
	function updatePhotoColorTransform():Void{
		var cxform:Object = new Object();
		//multiplier
		cxform.ra = __controls[0].value;
		cxform.ga = __controls[1].value;
		cxform.ba = __controls[2].value;
		cxform.aa = __controls[3].value;
		//offset
		cxform.rb = 0;
		cxform.gb = 0;
		cxform.bb = 0;
		cxform.ab = 0;
		
		var c:Color = new Color(__loader);
		c.setTransform(cxform);
		c = null;
	}
	
	function startAutoTransform():Void{
		this["angle_r"] = __controls[0].value/50 - 1;
		this["angle_g"] = __controls[1].value/50 - 1;
		this["angle_b"] = __controls[2].value/50 - 1;

		this["speed_r"] = .1;
		this["speed_g"] = .2;
		this["speed_b"] = .3;
		
		onEnterFrame = onEnterFrame_autoTransform;
	}

	function stopAutoTransform():Void{
		onEnterFrame = null;
	}
	
	function onEnterFrame_autoTransform():Void{
		__controls[0].value = 50 + Math.sin(this["angle_r"]) * 50;
		__controls[1].value = 50 + Math.sin(this["angle_g"]) * 50;
		__controls[2].value = 50 + Math.sin(this["angle_b"]) * 50;
		
		this["angle_r"] += this["speed_r"];
		this["angle_g"] += this["speed_g"];
		this["angle_b"] += this["speed_b"];
		
		updatePhotoColorTransform();
	}	
}
